package com.tuojiahr.action;

import java.io.IOException;
import java.util.List;

import com.opensymphony.xwork2.Preparable;
import com.tuojiahr.common.BaseActionCommon;
import com.tuojiahr.domain.TuoJiaHrQQContact;
import com.tuojiahr.domain.TuoJiaHrUser;
import com.tuojiahr.global.Constants;
import com.tuojiahr.service.QQService;
import com.tuojiahr.tools.StringUtil;

/**
 * 说明：
 *
 * @author wang.haibo
 * @date 2012-11-12 上午1:12:10
 */

public class QQAction extends BaseActionCommon implements Preparable {

	private static final long serialVersionUID = 1L;
	
	private QQService qqService;
	
	private String url = "adminmgr/qq_getQQList";
	private String message = "出错啦！！！";
	
	private TuoJiaHrQQContact qq;
	
	private TuoJiaHrUser user;

	@Override
	public void prepare() throws Exception {
		user = getCurrUser();
	}
	
	/**
	 * 获取qq列表
	 * @return
	 */
	public String getQQList() {
		if(null != user) {
			try {
				List<TuoJiaHrQQContact> qqList = qqService.getQqList();
				request.setAttribute("qqList", qqList);
				return "qq_list_view";
			} catch(Exception e) {
				Constants.ExceptionLog.error("##QQAction-----getQQList方法查询出错:", e);
			}
		}
		return "msg";
	}
	
	/**
	 * 打开添加或更新窗口
	 * @return
	 */
	public String openAddOrUpdate() {
		if(null != user) { 
			String qqId = request.getParameter("qqId");
			if(StringUtil.isNotBlank(qqId)) {
				qq = qqService.getQqById(Long.valueOf(qqId));
				url = "adminmgr/qq_updateQQ";
			} else {
				url = "adminmgr/qq_addQQ";
			}
			return "add_update_qq";
		}
		return "msg";
	}

	/**
	 * 添加
	 */
	public String addQQ() {
		if(null != user) { 
			qq.setOperator(user);
			try {
				qqService.addQq(qq);
				return "action";
			} catch (Exception e) {
				Constants.ExceptionLog.error("##QQAction---addQQ方法添加出错:", e);
			}
		} 
		return "msg";
	}
	
	/**
	 * 编辑
	 */
	public String updateQQ() {
		if(null != user) { 
			if(null != qq && 0 != qq.getId()) {
				qq.setOperator(user);
				try {
					qqService.updateQq(qq);
					return "action";
				} catch (NumberFormatException e) {
					Constants.ExceptionLog.error("##QQAction---updateQQ方法更新出错:", e);
				}
			}
		}
		return "msg";
	}
	
	/**
	 * 删除，支持批量
	 * @throws IOException 
	 */
	public void delQQById() throws IOException {
		String result = "fault";
		String ids = request.getParameter("ids");
		if(StringUtil.isNotBlank(ids)) {
			try {
				qqService.deleteQq(ids);
				result = "success";
			} catch (Exception e) {
				result = "error";
				Constants.ExceptionLog.error("##QQAction---delQQById方法删除出错:", e);
			}
		}
		response.getWriter().print(result);
		response.getWriter().close();
	}
	
	public void setQqService(QQService qqService) {
		this.qqService = qqService;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public TuoJiaHrQQContact getQq() {
		return qq;
	}

	public void setQq(TuoJiaHrQQContact qq) {
		this.qq = qq;
	}

}
